import { LoginRespones } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import { http } from '@/utils/http'
import { clearToken, setToken } from '@/utils/token'

// 传入参数类型
type LoginParams = {
  mobile: string
  code: string
}



// 登录获取Token
export const getLogin = (data: LoginParams): RootThunkAction => {
  return async (dispatch) => {
    //  <LoginData>为该接口的返回指定类型
    const res = await http.post<LoginRespones>('/authorizations', data)
    const { data: token } = res.data
    setToken(token)
    dispatch({ type: 'login/getToken', payload: res.data.data })
  }
}

// 发送验证码
export const getCode = (moubile: string) => {
  return async () => {
    await http.get(`/sms/codes/${moubile}`)
  }
}

// 退出登录
export const Logout = ():RootThunkAction => {
  return dispatch => {
    // 清除本地与redux中的token
    clearToken()

    dispatch({type:'login/Logout'})
  }
}